
package com.fitns.user.controller.baseConfig;

import com.alibaba.fastjson.JSON;

import com.fitns.user.conf.Resp;
import com.fitns.user.constant.Constants;
import com.fitns.user.dto.baseConfig.CollectDataCondition;
import com.fitns.user.dto.baseConfig.CollectDataList;
import com.fitns.user.dto.baseConfig.CollectDataVo;
import com.fitns.user.model.baseConfig.TbCollectData;
import com.fitns.user.service.baseConfig.CollectDataService;
import com.fitns.user.service.pub.FileService;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.ResultData;
import com.fitns.user.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *Created by zhou on 2018/7/25
 *
 */
@RestController
@RequestMapping("/CollectData")
@CrossOrigin
@Api(description = "收集点数据")
public class CollectDataController {


    @Autowired
    private CollectDataService collectDataService;

    @Autowired
    private FileService fileService;


    @GetMapping("/search")
    @ApiOperation("列表查询 ")
    public Object search( CollectDataList tbCollectData){
        PageInfo<CollectDataList> CollectDataList= collectDataService.search(tbCollectData);
//        CollectDataList.setPageSize(tbCollectData.getPageSize());
        return Resp.page(CollectDataList);
    }

    @GetMapping("/findDetailed")
    @ApiOperation("查看")
    public ResultData findDetailed(CollectDataCondition tbCollectData){
        TbCollectData CollectData= collectDataService.findDetailed(tbCollectData);
        if(StringUtils.isEmpty(CollectData)){
            return new ResultData(300,"DEFAULT",CollectData,"");
        }
        return new ResultData(200,"SUCCESS",CollectData,"");

    }

    @DeleteMapping("")
    @ApiOperation("删除")
    public ResultData delete(@RequestParam("id")Long id){
        collectDataService.delete(id);
        return new ResultData(200,"SUCCESS",null,"");
    }

    @GetMapping("/edit")
    @ApiOperation("修改")
    public ResultData modify(@RequestBody TbCollectData tbCollectData){
//        collectDataService.modify(tbCollectData);
        TbCollectData CollectData=collectDataService.modify(tbCollectData);
        if(StringUtils.isEmpty(CollectData)){
            return new ResultData(300,"DEFAULT",CollectData,"");
        }
        return new ResultData(200,"SUCCESS",CollectData,"");

    }

    @PostMapping("/insert")
    @ApiOperation("新增")
    public ResultData insert(@RequestBody TbCollectData tbCollectData){
        TbCollectData CollectData= collectDataService.insert(tbCollectData);
        return new ResultData(200,"SUCCESS",CollectData,"");
    }




    /** 图片上传功能 */
    @ApiOperation("上传图片并新增或修改相应信息")
    @PostMapping("updateFile")
    public String insertImage(TbCollectData CollectData,  @RequestParam MultipartFile[] pic) throws Exception {
        String images="";
        // 把图片保存到图片目录下
//        if(Validator.isNotNull(pic)) {
        for (int i=0;i<pic.length;i++) {
            MultipartFile file = pic[i];
            // 为每个文件生成一个新的文件名
            String picName = UuidUtils.getUUID();
            // 取文件扩展名
            String oriName = file.getOriginalFilename();
            String extName = oriName.substring(oriName.lastIndexOf("."));
            // 保存文件
            file.transferTo(new File(Constants.IMAGE_PATH + picName + extName));
            images += Constants.PATH+picName + extName + ";";
        }
//        }
        if(images!=""){
            images=images.substring(0,images.length()-1);
        }
        // 把文件名保存到数据库
        CollectData.setImage(images);
        collectDataService.insertImage(CollectData);
        return CollectData.getImage();

    }

    /**
      * IO流读取图片 by:long
      * @return
      */
    @GetMapping("/downLoadImage")
    public String IoReadImage(String imgName,HttpServletRequest request,HttpServletResponse response) throws IOException {
        return fileService.IoReadImage(imgName, request, response);
    }
    @GetMapping("/exportCollectData")
    @ApiOperation("导出收集点")
    public void exportCustomer(CollectDataCondition collectDataCondition, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<List<String>> excelList =collectDataService.export(collectDataCondition);
        ExcelUtils.export(response,request,"收集点数据列表",excelList);
    }
}
